<template>
	<view class="u-page">
		<view class="u-demo-block">
			<u--form
				:labelWidth="80"
				:model="formData"
				:rules="formRules"
				ref="form">
				<!-- 输入账户 -->
				<u-form-item
					label="账户号"
					required
					prop="userInfo.userCode"
					ref="userCode">
					<u-input
						v-model="formData.userInfo.userCode"
						clearable
						:disabled="disabled.userCode"
						:focus="focus.userCode"
						@focus="userCodeFocus"
						@blur="userCodeBlur()">
					</u-input>				
				</u-form-item>
				<view class="echo">
					<u--text
						type="success"
						:text="echo.userCode">
					</u--text>
				</view>
				<!-- 输入设备 -->
				<u-form-item
					label="设备号"
					required
					prop="machineInfo.machineNo"
					ref="machineNo">
					<u-input
						v-model="formData.machineInfo.machineNo"
						clearable
						:disabled="disabled.machineNo"
						:focus="focus.machineNo"
						@focus="machineNoFocus"
						@blur="machineNoBlur()">
					</u-input>				
				</u-form-item>
				<view class="echo">
					<u--text
						type="success"
						wordWrap="break-word"
						:lines="3"
						:text="echo.machineNo">
					</u--text>
				</view>
				<u-line :customStyle="{marginTop:'5px'}"></u-line>
				<!-- 记录的内容 -->
				<view
					v-show="show.otherRecordItem">
					<!-- 作业项目 -->
					<u-form-item
						label="作业项目"
						prop="opItemName"
						ref="opItem"
						required
						@click="opItemFormItemClick">
						<u-input
							v-model="formData.opItemName"
							style="pointer-events: none"
							disabled
							disabledColor="#ffffff"
							placeholder="请选择作业项目"
							fontSize="13px"
							color="#12569F"
							suffixIcon="arrow-right">
						</u-input>
					</u-form-item>
					
					<!-- 作业记录 -->
					<u-form-item
						v-show="show.formItemShow.recordItem"
						:label="formItemRecordLabel"
						prop="recordValue"
						ref="recordItem">
						<!-- 文本域 -->
						<u--textarea
							v-model="formData.recordValue"
							v-show="show.formItemShow.recordItemInner.textarea"
							:placeholder="textPlaceholder"
							autoHeight
							count
							:maxlength="144">
						</u--textarea>
						<!-- input输入框 -->
						<u-input
							v-model="formData.recordValue"
							v-show="show.formItemShow.recordItemInner.input"
							:placeholder="textPlaceholder"
							clearable
							:disabled="disabled.textInputItem"
							@clear="textInputIteClear"
							@focus="textInputItemFocus"
							@blur="textInputItemBlur">
						</u-input>
						<!-- 单选框 -->
						<u-radio-group
							v-show="show.formItemShow.recordItemInner.radio"
							v-model="formData.recordValue"
							placement="column"
							@change="radioChange">
							<u-radio
								v-for="(item,index) in selectedOpItemPojo.maintsItemContent"
								:key="index"
								:label="item"
								:name="item"
								:customStyle="{marginLeft:'80px',marginBottom: '10px'}">
								
							</u-radio>
						</u-radio-group>
						<!-- 多选框 -->
						<u-checkbox-group
							v-show="show.formItemShow.recordItemInner.checkbox"
							v-model="checkboxOpRecordValueArr"
							placement="column"
							@change="checkboxChange">
							<u-checkbox
								v-for="(item,index) in selectedOpItemPojo.maintsItemContent"
								:key="index"
								:label="item"
								:name="item"
								:customStyle="{marginLeft:'80px',marginBottom: '10px'}">
								
							</u-checkbox>
						</u-checkbox-group>
					</u-form-item>
					<view class="echo">
						<u--text
							type="success"
							wordWrap="break-word"
							:lines="2"
							:text="echo.qrPn"
							:customStyle="{marginBottom:'8px'}">
						</u--text>
						<u--text
							type="success"
							wordWrap="break-word"
							:lines="2"
							:text="echo.qrLotNo">
						</u--text>
					</view>
					<u-line></u-line>
					
					<!-- 记录开始时间 -->
					<u-form-item
						v-show="show.beginTimeItem"
						label="记录开始\n时间"
						required
						prop="timeInfo.begin"
						ref="timeInfoBegin"
						@click="show.timePicker.begin=true">
						<u-input
							v-model="formData.timeInfo.begin"
							style="pointer-events: none"
							disabled
							disabledColor="#ffffff"
							placeholder="请选择记录开始时间"
							fontSize="13px"
							color="#12569F"
							>
						</u-input>
						<view slot="right" style="margin-left: 8px;" @click.stop="">
							<u-switch 
								v-model="beginTimeSwitch" 
								:disabled="disabled.beginTimeSwitch"
								size="20"
								@change="beginTimeSwitchChange">
							</u-switch>
						</view>
					</u-form-item>
					<u-line></u-line>
					<!-- 记录结束时间 -->
					<u-form-item
						v-show="show.endTimeItem"
						label="记录结束\n时间"
						required
						prop="timeInfo.end"
						ref="timeInfoEnd"
						@click="show.timePicker.end=true">
						<u-input
							v-model="formData.timeInfo.end"
							style="pointer-events: none"
							disabled
							disabledColor="#ffffff"
							placeholder="请选择记录结束时间"
							fontSize="13px"
							color="#12569F">
						</u-input>
						<view slot="right" style="margin-left: 8px;" @click.stop="">
							<u-switch 
								v-model="endTimeSwitch" 
								:disabled="disabled.endTimeSwitch"
								size="20"
								@change="endTimeSwitchChange">
							</u-switch>
						</view>
					</u-form-item>
					<u-line></u-line>
					<!-- 输入选择列 -->
					<u-form-item
						label="选择列"
						prop="checkboxColumnsNoArr"
						ref="columnNo">
						<u-checkbox-group
							v-model="formData.checkboxColumnsNoArr"
							placement="column"
							:disabled="disabled.checkboxColumnsNo"
							@change="columnsNoChange">
							<u-checkbox
								v-for="(item,index) in selectedOpItemPojo.mutiColCount"
								:key="index"
								:label="item"
								:name="item"
								:customStyle="checkboxCustomStyle">
							</u-checkbox>
						</u-checkbox-group>
						
					</u-form-item>
					<u-line :customStyle="{marginTop:'5px'}"></u-line>
					<!-- 输入部位 -->
					<u-form-item
						v-show="show.partItem"
						label="设备部位"
						prop="partArr"
						ref="part">
						<u-checkbox-group
							v-model="formData.partArr"
							placement="column"
							:disabled="disabled.part"
							@change="partChange">
							<u-checkbox
								v-for="(item,index) in selectedOpItemPojo.partAllArr"
								:key="index"
								:label="item.partName"
								:name="item.partName"
								:customStyle="checkboxCustomStyle2">
							</u-checkbox>
						</u-checkbox-group>
						
					</u-form-item>
					<u-line
						v-show="show.partItem"
						:customStyle="{marginTop:'5px'}"></u-line>
					<!-- 扫描LOT信息 -->
					<u-form-item
						label="LOT信息"
						prop="lotInfo"
						ref="lotInfo">
						<u--textarea
							v-model="formData.lotInfo"
							placeholder="按需输入或扫描"
							autoHeight
							count
							:maxlength="144"
							:disabled="disabled.lotInfo">
							
						</u--textarea>
					</u-form-item>
					<!-- 输入备注 -->
					<u-form-item
						label="备注"
						prop="remark"
						ref="remark">
						<u--textarea
							v-model="formData.remark"
							placeholder="按需输入"
							autoHeight
							count
							maxlength="144"
							:disabled="disabled.remark">
							
						</u--textarea>
					</u-form-item>
					<!-- 输入确认者 -->
					<u-form-item
						label="确认"
						prop="operationVerify"
						ref="operationVerify">
						<u-input
							v-model="formData.operationVerify"
							clearable
							placeholder="按需输入确认者姓名或账号"
							:disabled="disabled.operationVerify">
							
						</u-input>
					</u-form-item>
				</view>
				
			</u--form>
			<!-- 按钮 -->
			<u-button
				type="primary"
				:text="submitBtn.text"
				customStyle="margin-top: 20px"
				:loading="submitBtn.loading"
				:disabled="submitBtn.disabled"
				loadingMode="semicircle"
				@click="formSubmit">
			</u-button>

			<u-button
				type="warning"
				text="重置"
				plain
				customStyle="margin-top: 20px"
				@click="formReset">
			</u-button>
		</view>
		<my-menu-drawer ref="myMenuDrawer"></my-menu-drawer>
		<!-- 作业项目选择器 -->
		<u-picker 
			:show="show.clickShow.opItemPicker" 
			:columns="opItemPickerList"
			:closeOnClickOverlay="true"
			keyName="maintsItemName"
			@confirm="opItemPickerConfirm"
			@close="show.clickShow.opItemPicker=false"
			@cancel="show.clickShow.opItemPicker=false">
		</u-picker>
		<!-- 开始时间选择器 -->
		<u-datetime-picker
			v-model="formData.timeInfo.begin"
			:show="show.timePicker.begin"
			mode="datetime"
			closeOnClickOverlay
			:minDate="1672502400000"
			:formatter="timePickerFormatter"
			@cancel="show.timePicker.begin=false"
			@close="show.timePicker.begin=false"
			@confirm="beginTimePickerConfirm"
		></u-datetime-picker>
		<!-- 结束时间选择器 -->
		<u-datetime-picker
			v-model="formData.timeInfo.end"
			:show="show.timePicker.end"
			mode="datetime"
			:formatter="timePickerFormatter"
			closeOnClickOverlay
			:minDate="endTimePickerMinDate"
			@cancel="show.timePicker.end=false"
			@close="show.timePicker.end=false"
			@confirm="endTimePickerConfirm"
		></u-datetime-picker>
	</view>
</template>

<script>
import {getUserCodeEcho} from '@/api/user/user.js';
import {getMachineNoEcho} from '@/api/machine/machine.js'
import {getMaintsItemListByMachineNo} from '@/api/maintsitem/maintsitem.js';
import {getQrType} from '@/api/qrtype/qrtype.js'
import {getPartByMachineNo} from '@/api/part/part.js'

import {addRecord,getUnfinishedRecord,uptUnfinishedRecord} from '@/api/record/record.js';
import toast from '../../uni_modules/uview-ui/libs/config/props/toast';
export default {
	data() {
		return {
			// 初始值
			formData:{
				userInfo:{
					userCode:''
				},
				machineInfo:{
					machineNo:''
				},
				opItemName:'',
				recordValue:'',
				// columnsNoValue:'',
				// 设备部位
				partArr:[],
				// 选择列时的数组
				checkboxColumnsNoArr:[],
				remark:'',
				operationVerify:'',
				lotInfo:'',
				timeInfo:{
					// begin: Number(new Date()),
					begin: uni.$u.timeFormat(Number(new Date()),'yyyy-mm-dd hh:MM'),
					end: uni.$u.timeFormat(Number(new Date())+60000,'yyyy-mm-dd hh:MM')
				}
			},
			// 表单校验规则
			formRules:{
				'userInfo.userCode':[
					{
						type: 'string',
						required: true,
						message: '请填写姓名',
						trigger: ['blur', 'change'],
					},
					{
						asyncValidator:(rule,value,callback)=>{
							if(uni.$u.test.isEmpty(value)){
								callback(new Error('不能为空'))
								return
							}
							getUserCodeEcho({userCode:value}).then(res=>{
								this.echo.userCode=res.data.departmentsPO.departmentName + '\n'+ res.data.usersPO.userFullName
								this.disabled.machineNo=false
								this.disabled.userCode=true
								
								uni.$u.sleep(300).then(()=>{
									this.focus.machineNo=true
									this.machineNoFocus()
								})
								callback()
							}).catch(err=>{
								callback(new Error(err.message|| err.msg || (err.errMsg+'; 服务器异常')))
							})
						},
						// trigger:['blur']
					}
				],
				'machineInfo.machineNo':[
					{
						type: 'string',
						required: true,
						message: '请填写设备号',
						trigger: ['blur', 'change']
					},
					{
						asyncValidator:(rule,value,callback)=>{
							if(uni.$u.test.isEmpty(value)){
								callback(new Error('不能为空'))
								return
							}
							let params = {
								machineNo: value,
								userCode : this.formData.userInfo.userCode
							}
							getMachineNoEcho(params).then(res=>{
								uni.$u.sleep(50).then(()=>{
									// this.echo.machineNo=res.data.topDept.topDeptName+'\n' + res.data.secDept.secDeptName + '\n' + res.data.machine.machineName
									this.echo.machineNo=res.data
									this.disabled.machineNo=true
									this.show.otherRecordItem=true
								})
								callback()
							}).catch(err=>{
								callback(new Error(err.message|| err.msg || (err.errMsg+'; 服务器异常')))
							})
						},
					}

				],
				opItemName:[
					{
						required: true, 
						message: '不能为空',
						trigger: ['change','blur'],
					},
				],
				//记录内容的验证方法
				recordValue:[
					{
						required: true, 
						message: '不能为空',
						trigger: ['change','blur'],
					},
					{
						validator:(rule,value,callback)=>{
							if(uni.$u.test.isEmpty(value)){
								return false
							}
							return true
						},
						message:'至少选一项',
						trigger: ['change','blur'],
					},
				],
				// 选择部位
				// partArr:[
				// 	{
				// 		type:'array',
				// 		min:1,
				// 		required:true,
				// 		message:'至少选一项',
				// 		trigger:['change']
				// 	}
				// ],
				
				// 选择列
				// checkboxColumnsNoArr:[
				// 	{
				// 		type:'array',
				// 		min:1,
				// 		required:true,
				// 		message:'至少选一项',
				// 		trigger:['change']
				// 	}
				// ],
				// 记录开始时间
				'timeInfo.begin':[
					{
						type: 'string',
						required: true,
						message: '请填写记录开始时间',
						trigger: ['blur']
					},
				],
				// 记录结束时间
				'timeInfo.end':[
					{
						type: 'string',
						required: true,
						message: '请填写记录结束时间',
						trigger: ['blur','change']
					},
					{
						validator: (rule,value,callback)=>{
							let begintime=this.formData.timeInfo.begin
							let endtime=value
							// 当时间不是时间戳时(非数字时)
							if(!(/^[0-9]+.?[0-9]*$/.test(begintime))){
								begintime = Number(new Date(begintime))
							}
							if(!(/^[0-9]+.?[0-9]*$/.test(endtime))){
								endtime = Number(new Date(endtime))
							}
							if(endtime>begintime){
								return true
							}
							return false
						},
						message: '结束时间不能小于开始时间',
					}
				],
			},
			//回显
			echo:{
				userCode : '',
				machineNo: '',
				qrPn:'',
				qrLotNo:'',
			},
			//显示
			show:{
				formItemShow:{		//form项目的显示
					recordItem:false,
					recordItemInner:{
						input: false,
						textarea:false,
						radio: false,
						checkbox:false,
					},
				},
				otherRecordItem:false,
				clickShow:{		// 点击时触发的显示
					opItemPicker:false
				},
				timePicker: {
					begin:false,
					end:false
				},
				beginTimeItem:true,
				endTimeItem:true,
				partItem:true,
			},
			//焦点
			focus:{
				userCode:false,
				machineNo:false
			},
			// 使能
			disabled:{
				userCode:false,
				machineNo:true,
				textInputItem: false,
				part:true,
				checkboxColumnsNo:true,
				remark:true,
				operationVerify:true,
				lotInfo:true,
				beginTimeSwitch:false,
				endTimeSwitch:false
			},
			opItemPickerList:[
				[]
			],
			selectedOpItemPojo:{},
			formItemRecordLabel:'',
			textPlaceholder:'',
			//qrtype的原型
			qrType:{},
			// 作业时多选时的数组
			checkboxOpRecordValueArr:[],
				
			// checkbox样式
			checkboxCustomStyle:{
				marginBottom:'0px'
			},
			checkboxCustomStyle2:{
				marginBottom:'0px'
			},

			recordSelectCustomStyle:{
				marginLeft:'80px',
				marginBottom: '0px'
			},
			// 提交按钮的属性
			submitBtn:{
				text:'提交',
				loading:false,
				disabled:false
			},
			timer:{
				userCode:0,
				machineNo:0
			},
			beginTimeSwitch: true,
			endTimeSwitch: true
		};
	},
	onShow() {
		this.$nextTick(()=>{
			this.focus.userCode=true
		})
	},
	onUnload(){
		clearTimeout(this.timer.userCode)
		clearTimeout(this.timer.machineNo)
	},
	onNavigationBarButtonTap(e) {
		if (e.type === 'menu') {
			this.showMenuDrawer();
		} else if(e.type==='home') {
			uni.reLaunch({
				url:'/pages/index/index'
			})
		}
	},
	computed:{
		endTimePickerMinDate(){
			return (new Date(this.formData.timeInfo.begin).getTime())+60000
		}
	},
	
	methods: {
		userCodeBlur(){
			this.timer.userCode= setTimeout(()=>{
				this.$refs.form.validateField('userInfo.userCode');
			},666)
			
		},
		machineNoBlur(){
			this.timer.machineNo= setTimeout(()=>{
				this.$refs.form.validateField('machineInfo.machineNo');
			},666)
		},
		showMenuDrawer() {
			this.$refs.myMenuDrawer.$refs.showLeft.open();
		},
		userCodeFocus(){
			uni.$u.sleep(100).then(()=>{
				// #ifdef APP-PLUS
				uni.hideKeyboard()
				// #endif
				if(this.echo.userCode!=''){
					this.echo.userCode=''
				}
			})
		},
		machineNoFocus(){
			uni.$u.sleep(300).then(()=>{
				// #ifdef APP-PLUS
				uni.hideKeyboard()
				// #endif
				if(this.echo.machineNo!=''){
					this.echo.machineNo=''
				}
			})
		},
		/**
		 * 作业项目的表单子项 点击事件
		 */
		opItemFormItemClick(){		
			this.$refs.form.validateField('userInfo.userCode')
			this.$refs.form.validateField('machineInfo.machineNo')
			// 获取菜单
			getMaintsItemListByMachineNo({machineNo:this.formData.machineInfo.machineNo})
				.then(res=>{
					let tempList=[]
					res.data.forEach(item=>{
						tempList.push(item)
					})
					
					this.opItemPickerList= [tempList]
					
					// 显示菜单,隐藏键盘
					uni.$u.sleep(100).then(()=>{
						this.show.clickShow.opItemPicker=true;
						// #ifdef APP-PLUS
						uni.hideKeyboard()
						// #endif
					})
				})
				.catch(err=>{
					uni.showToast({
						title:err.msg ||(err.errMsg+': 服务器异常'),
						icon:'error'
					})
				})
		
		},
		/**
		 * 作业项目中picker确认按钮点击事件
		 * @param {Object} selectedOpItem 选中的对象
		 */
		opItemPickerConfirm(selectedOpItem){
			// 初始化数据
			this.initData()
			
			// 选中值 selectedOpItem.value 是一个数组
			if(uni.$u.test.array(selectedOpItem.value)){
				// 作业项目名称赋值
				this.formData.opItemName=selectedOpItem.value[0].maintsItemName
				// 临时保存一下选中的作业项目
				this.selectedOpItemPojo=selectedOpItem.value[0]
			}
			// 设置选择列的格式
			if(!(uni.$u.test.isEmpty(this.selectedOpItemPojo.mutiColCount*1))){
				
				// 当选择列条数大于1时
				if((this.selectedOpItemPojo.mutiColCount*1)>1) {
					this.checkboxCustomStyle.marginBottom='8px'
				} else {
					this.checkboxCustomStyle.marginBottom='0px'
				}
				let tempArr=[]
				for(let i=0;i<this.selectedOpItemPojo.mutiColCount;i++){
					tempArr.push('第'+(i+1)+'列')
				}
				this.selectedOpItemPojo.mutiColCount=tempArr
				this.disabled.checkboxColumnsNo=false
		
				// this.formData.checkboxColumnsNoArr[0]='第1列'
				// this.formData.columnsNoValue=this.checkboxColumnsNoArr.join()
			}
			
			// 设备部位
			getPartByMachineNo({machineNo:this.formData.machineInfo.machineNo})
				.then(res=>{
					this.selectedOpItemPojo.partAllArr=res.data
					
					if(uni.$u.test.array(this.selectedOpItemPojo.partAllArr) && this.selectedOpItemPojo.partAllArr.length>1) {
						this.checkboxCustomStyle2.marginBottom='8px'
					} else {
						this.checkboxCustomStyle2.marginBottom='0px'
					}
					
					// 设置设备部位相关
					this.disabled.part=false
				})
				.catch(err=>{
					// 230309 部位可以为空
					// uni.showToast({
					// 	title:err.msg ||(err.errMsg+': 服务器异常'),
					// 	icon:'error'
					// })
					this.show.partItem=false	
				})
			
			// 设置lotInfo相关
			this.disabled.lotInfo=false
			// 设置备注相关
			this.disabled.remark=false
			// 设置确认者相关
			this.disabled.operationVerify=false
			
			// 关闭项目选择器
			this.show.clickShow.opItemPicker=false
		
			//显示需要记录的内容
			this.recordShow();			
		},
		/**
		 * 显示需要记录的内容
		 */
		recordShow(){
			console.log(this.selectedOpItemPojo);
			// 显示记录的form item
			this.show.formItemShow.recordItem=true
		
			// 如果记录方法是文本时
			if(this.selectedOpItemPojo.maintsItemContentType=='text'){
				// input项的label赋值
				this.formItemRecordLabel=this.selectedOpItemPojo.maintsItemContent
				if(this.selectedOpItemPojo.qrType=="textstr"){
					this.textPlaceholder="请直接输入"	
					this.show.formItemShow.recordItemInner.textarea=true
				} else {
					this.textPlaceholder="请扫描"
					this.show.formItemShow.recordItemInner.input=true
				}
			}
			// 如果记录方法是单选或多选
			// else if(this.selectedOpItemPojo.opItemContentType=='select'){
			else {
				if(uni.$u.test.isEmpty(this.selectedOpItemPojo.maintsItemContent)){
					uni.showToast({
						title:'系统设定异常',
						icon:'error'
					})
					uni.$u.sleep(1500).then(()=>{
						return
					})
				}
				// 按;分割字符串
				this.selectedOpItemPojo.maintsItemContent = this.selectedOpItemPojo.maintsItemContent.split(/;|；/i)
				if(uni.$u.test.array(this.selectedOpItemPojo.maintsItemContent) && this.selectedOpItemPojo.maintsItemContent.length>1){
					this.recordSelectCustomStyle.marginLeft='80px'
					this.recordSelectCustomStyle.marginBottom='10px'
				}
				if(this.selectedOpItemPojo.maintsItemContentType=='select'){
					// 默认第一个值被选中
					uni.$u.sleep(50).then(()=>{
						this.formData.recordValue=this.selectedOpItemPojo.maintsItemContent[0]
						this.show.formItemShow.recordItemInner.radio=true
					})
					this.echo.qrPn='单选↑'
				} else {
					// 默认第一个值被选中
					uni.$u.sleep(50).then(()=>{
						this.checkboxOpRecordValueArr=[this.selectedOpItemPojo.maintsItemContent[0]]
						this.formData.recordValue=this.checkboxOpRecordValueArr.join(';')
						this.show.formItemShow.recordItemInner.checkbox=true
					})

					this.echo.qrPn='多选↑'
				}
		
			}
		},
		textInputItemFocus(){
			uni.$u.sleep(100).then(()=>{
				// #ifdef APP-PLUS
				uni.hideKeyboard()
				// #endif
			})
		},
		// 文本输入框失焦事件
		textInputItemBlur(val){
			if(uni.$u.test.isEmpty(this.selectedOpItemPojo.qrType)){
				uni.showToast({
					title:'系统设定异常',
					icon:'error'
				})
				return ;
			}
			getQrType({qrTypeID:this.selectedOpItemPojo.qrTypeID})
				.then(res=>{
					this.qrType=res.data
					if(uni.$u.test.isEmpty(this.formData.recordValue)){return}
					this.formData.recordValue=val.substr(this.qrType.pnFrom,this.qrType.pnLength).trim() + " | "+ val.substr(this.qrType.lotNoFrom,this.qrType.lotNoLength).trim()
					// this.disabled.textInputItem= true
					this.echo.qrPn="型名: " + val.substr(this.qrType.pnFrom,this.qrType.pnLength).trim()
					this.echo.qrLotNo="LotNo: " + val.substr(this.qrType.lotNoFrom,this.qrType.lotNoLength).trim() 
				})
				.catch(err=>{
					uni.showToast({
						title:err.msg,
						icon:'error'
					})
					return
				})
				
		},
		textInputIteClear(){
			uni.$u.sleep(100).then(()=>{
				this.formData.recordValue=''
				this.echo.qrPn=''
				this.echo.qrLotNo=''
			})
		},
		radioChange(selected){
			this.formData.recordValue=selected
		},
		checkboxChange(selected){
			this.checkboxOpRecordValueArr=selected
			this.formData.recordValue=this.checkboxOpRecordValueArr.join(';')
		},
		columnsNoChange(selected){
			this.formData.checkboxColumnsNoArr=selected
			// this.formData.columnsNoValue=this.checkboxColumnsNoArr.join(';')
		},
		partChange(selected){
			this.formData.partArr=selected
		},
		/**
		 * 时间选择器
		 */
		timePickerFormatter(type,value){
			if (type === 'year') {
				return `${value}年`
			}
			if (type === 'month') {
				return `${value}月`
			}
			if (type === 'day') {
				return `${value}日`
			}
			if (type === 'hour'){
				return `${value}点`
			}
			if (type === 'minute'){
				return `${value}分`
			}
			
			return value
		},
		beginTimePickerConfirm(e){
			this.$nextTick(()=>{
				this.formData.timeInfo.begin=uni.$u.timeFormat(e.value,'yyyy-mm-dd hh:MM')
				
			})
			this.show.timePicker.begin=false
		},
		endTimePickerConfirm(e){

			this.$nextTick(()=>{
				this.formData.timeInfo.end=uni.$u.timeFormat(e.value,'yyyy-mm-dd hh:MM')
			})
			this.show.timePicker.end=false
			this.$refs.form.validateField('timeInfo.end')
		},
		/**
		 * 初始化数据
		 */
		initData(){
			// 清空opItemPojo对象
			this.selectedOpItemPojo={}
			// 清空其他属性
			this.formItemRecordLabel=''
			this.textPlaceholder=''
			
			// 清空一般数据
			this.formData.opItemName=''
			this.formData.recordValue=''
			// this.formData.columnsNoValue=''
			this.formData.lotInfo=''
			this.formData.remark=''
			this.formData.operationVerify=''
			this.checkboxOpRecordValueArr=[]
			this.formData.partArr=[]
			this.formData.checkboxColumnsNoArr=[]
			this.formData.timeInfo.begin=uni.$u.timeFormat(Number(new Date()),'yyyy-mm-dd hh:MM').toString()
			this.formData.timeInfo.end=uni.$u.timeFormat(Number(new Date())+60000,'yyyy-mm-dd hh:MM')
			
			// 全部返回不显示
			this.show.formItemShow.recordItem=false
			this.show.formItemShow.recordItemInner.input=false
			this.show.formItemShow.recordItemInner.textarea=false
			this.show.formItemShow.recordItemInner.radio=false
			this.show.formItemShow.recordItemInner.checkbox=false
			
			// 230309 只入开始时间或结束时间做的改善
			this.show.beginTimeItem=true
			this.show.endTimeItem=true
			this.beginTimeSwitch=true
			this.endTimeSwitch=true
			this.show.partItem=true
			this.disabled.beginTimeSwitch=false
			this.disabled.endTimeSwitch=false
			
			// 使能
			this.disabled.textInputItem= false
			this.disabled.checkboxColumnsNo=true
			this.disabled.part=true
			this.disabled.remark=true
			this.disabled.operationVerify=true
			this.disabled.lotInfo=true
			// 回显
			this.echo.qrPn=''
			this.echo.qrLotNo=''
			// 样式
			this.checkboxCustomStyle.marginBottom='0px'
			this.checkboxCustomStyle2.marginBottom='0px'
			this.recordSelectCustomStyle.marginLeft='80px'
			this.recordSelectCustomStyle.marginBottom='0px'
		},
		
		// 提交本页数据
		formSubmit(){
			const _this=this;
			// 数据验证
			this.$refs.form.validate()
				.then(res=>{		//成功时返回true
					uni.showModal({
						title:'上传',
						content:'确认数据上传?',
						success:(res)=>{
							if(res.confirm){
								// 230309 检查是否有任务未结束
								getUnfinishedRecord({machineNo:this.formData.machineInfo.machineNo})
									.then(res=>{
										//当返回有数据时,提示有任务
										if(!uni.$u.test.isEmpty(res.data)){
											let tmpstr=''
											res.data.forEach((item)=>{
												tmpstr+= item.recordType+ ' : ' + item.recordName + ' : ' + item.recordResult + '\n'
											})
											uni.showModal({
												title:'未完成任务',
												content:'有未完成的任务\n是否继续???\n'+ tmpstr,
												success(res) {
													if(res.confirm){
														// 上传数据
														_this.updateData()
													}
												}
											})											
										} else {
											_this.updateData()
										}
									
									})
									.catch(err=>{
										uni.$u.toast('服务异常1')
									})
								
							}
						}
						
					})
				})
				.catch(err=>{
					console.log(err);
					uni.showToast({
						title:'提交数据错误',
						icon:'error',
						duration:2000
					})
				})
		},
		updateData(){
			this.submitBtn.text="上传中..."
			this.submitBtn.disabled=true
			this.submitBtn.loading=true
			
			// console.log(utcTimeOffset);
			// let begintime = (/^[0-9]+.?[0-9]*$/.test(this.formData.timeInfo.begin))?this.formData.timeInfo.begin:new Date(this.formData.timeInfo.begin).getTime()
			// let endtime = (/^[0-9]+.?[0-9]*$/.test(this.formData.timeInfo.end))?this.formData.timeInfo.end:new Date(this.formData.timeInfo.end).getTime()

			let begintime = this.formData.timeInfo.begin+":00"
			let endtime = this.formData.timeInfo.end+":00"
			
			// 220309 追加结束时的员工号
			let userCode =this.formData.userInfo.userCode
			let userCodeEnd = this.formData.userInfo.userCode
			// 220309 时间可以删除
			if(this.show.beginTimeItem==false){
				begintime=null;
				userCode= ''
			}
			if(this.show.endTimeItem==false){
				endtime=null;
				userCodeEnd=''
			}
			
			
			let data = {
				userCode: userCode,
				userCodeEnd : userCodeEnd,
				machineNo: this.formData.machineInfo.machineNo,
				recordName: this.formData.opItemName,
				recordType: '保全作业',
				recordResult: this.formData.recordValue,
				recordTimeBeginDT: begintime,
				recordTimeEndDT:endtime,
				recordSelectedColNo: this.formData.checkboxColumnsNoArr.join(';'),
				recordPart:this.formData.partArr.join(';'),
				recordLotInfo: this.formData.lotInfo,
				recordRemark: this.formData.remark,
				recordVerify:this.formData.operationVerify,
			}
			//230309 当删除开始时间, 只录入结束时间时
			if(this.show.beginTimeItem==false){
				// 更新结束的数据
				uptUnfinishedRecord(data)
					.then(res=>{
						uni.showToast({
							title:'结束成功',
							duration:2000
						})						
						this.submitBtn.text="提交"
						this.submitBtn.disabled=false
						this.submitBtn.loading=false
						this.initData()				
					})
					.catch(err=>{
						uni.showToast({
							title: err.msg,
							icon:'error',
							duration:2000
						})
						this.submitBtn.text="提交"
						this.submitBtn.disabled=false
						this.submitBtn.loading=false
												
					})
				return 
			}
			addRecord(data).then(res=>{
				uni.showToast({
					title:'提交成功',
					duration:2000
				})
				this.submitBtn.text="提交"
				this.submitBtn.disabled=false
				this.submitBtn.loading=false
				this.initData()
			
			}).catch(err=>{
				uni.showToast({
					title: err.msg||' 服务器异常',
					icon:'error',
					duration:2000
				})
				this.submitBtn.text="提交"
				this.submitBtn.disabled=false
				this.submitBtn.loading=false
			})
		},
		// 重置按钮
		formReset(){

			this.$refs.form.resetFields();
			// 清空前两项
			this.disabled.userCode=false;
			this.disabled.machineNo=true;
			this.echo.userCode=''
			this.echo.machineNo=''
			this.userCodeFocus();
			// 清空后面的
			if(this.show.otherRecordItem){
				this.initData()
				this.echo.qrPn=''
				this.echo.qrLotNo=''
				this.formData.checkboxColumnsNoArr=[]		
				if(this.show.formItemShow.recordItemInner.input) {this.show.formItemShow.recordItemInner.input=false}
				if(this.show.formItemShow.recordItemInner.textarea){
					this.show.formItemShow.recordItemInner.textarea=false
				}
				if(this.show.formItemShow.recordItemInner.radio){
					this.show.formItemShow.recordItemInner.radio=false
				}
				if(this.show.formItemShow.recordItemInner.checkbox){
					this.show.formItemShow.recordItemInner.checkbox=false
				}
				this.show.otherRecordItem=false
						
			}
		},
		hideKeyboard(){
			// #ifdef APP-PLUS
			// uni.hideKeyboard()
			// #endif
		},
		beginTimeSwitchChange(e){
			if(e!=true){
				uni.showModal({
					title:'删除项目',
					content:'取消开始时间?',
					success:(res)=>{
						if(res.confirm){
							this.show.beginTimeItem=false
							this.disabled.endTimeSwitch=true
							this.formData.timeInfo.begin=uni.$u.timeFormat(1672502400000,'yyyy-mm-dd hh:MM')
						}else {
							this.beginTimeSwitch=true
						}
					}
				})
			}
		},
		endTimeSwitchChange(e){
			if(e!=true){
				uni.showModal({
					title:'删除项目',
					content:'取消结束时间?',
					success:(res)=>{
						if(res.confirm){
							this.show.endTimeItem=false
							this.disabled.beginTimeSwitch=true
						}else {
							this.endTimeSwitch=true
						}
					}
				})
			}
		}
	
	}
};
</script>

<style scoped lang="scss">
.echo {
	margin-left: 80px;
}
</style>
